package com.hns.can.master.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 整合各首页趋势图数据工具
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class TendencyChartsDataTools extends CommonService{
	
	/**
	 * 填充趋势图数据
	 * @param list
	 * @param dateType
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public List<Map<String, Object>> fillMultiTendencyData(List<Map<String, Object>> list, String dateType){
		List<Map<String, Object>> fullDate = getFullDate(dateType);
		//遍历1,获取时间段内类别
		HashSet<String> set = new HashSet<String>();
		Map<String, Map<String, Object>> sourceMap = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> map : list){
			set.add((String) map.get("TREND_VAL1"));
			String str1 = (String)map.get("REPORT_DATE");
			String str2 = (String)map.get("TREND_VAL1");
			sourceMap.put(str1+"_"+str2, map);
		}
		ArrayList<Map<String, Object>> fillList = new ArrayList<Map<String, Object>>();
		for(int dsize=0;dsize<fullDate.size();dsize++){
			Map<String, Object> dMap = fullDate.get(dsize);
			String date = (String)dMap.get("TREND_DATE");
			Iterator<String> it = set.iterator();
			while(it.hasNext()){
				String type = it.next();
				Map<String, Object> resultMap = sourceMap.get(date+"_"+type);
				if(PubUtil.isNotEmpty(resultMap)){
					fillList.add(resultMap);
				}else{
					Map<String, Object> tmp = new HashMap<String, Object>();
					tmp.put("REPORT_DATE", date);
					tmp.put("TREND_VAL1", type);
					tmp.put("TREND_VAL2", "0");
					fillList.add(tmp);
				}
			}
		}
		return fillList;
	}
	
	//TODO 待重构
	/**
	 * 填充安全趋势图数据
	 * @param list
	 * @param dateType
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public List<Map<String, Object>> fillSafetyMultiTendencyData(List<Map<String, Object>> list, String dateType){
		List<Map<String, Object>> fullDate = getFullDate(dateType);
		//遍历1,获取时间段内类别
		HashSet<String> set = new HashSet<String>();
		Map<String, Map<String, Object>> sourceMap = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> map : list){
			set.add((String) map.get("SAFE_TREND_ITEM"));
			String str1 = (String)map.get("REPORT_DATE");
			String str2 = (String)map.get("SAFE_TREND_ITEM");
			sourceMap.put(str1+"_"+str2, map);
		}
		ArrayList<Map<String, Object>> fillList = new ArrayList<Map<String, Object>>();
		for(int dsize=0;dsize<fullDate.size();dsize++){
			Map<String, Object> dMap = fullDate.get(dsize);
			String date = (String)dMap.get("TREND_DATE");
			Iterator<String> it = set.iterator();
			while(it.hasNext()){
				String type = it.next();
				Map<String, Object> resultMap = sourceMap.get(date+"_"+type);
				if(PubUtil.isNotEmpty(resultMap)){
					fillList.add(resultMap);
				}else{
					Map<String, Object> tmp = new HashMap<String, Object>();
					tmp.put("REPORT_DATE", date);
					tmp.put("SAFE_TREND_ITEM", type);
					tmp.put("SAFE_TREND_VAL", "0");
					fillList.add(tmp);
				}
			}
		}
		return fillList;
	}
	
	public List<Map<String, Object>> fillSingleTendencyData(List<Map<String, Object>> list, String dateType){
		List<Map<String, Object>> fullDate = getFullDate(dateType);
		Map<String, Map<String, Object>> sourceMap = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> map : list){
			String str1 = (String)map.get("REPORT_DATE");
			sourceMap.put(str1, map);
		}
		ArrayList<Map<String, Object>> fillList = new ArrayList<Map<String, Object>>();
		for(int dsize=0;dsize<fullDate.size();dsize++){
			Map<String, Object> dMap = fullDate.get(dsize);
			String date = (String)dMap.get("TREND_DATE");
			Map<String, Object> resultMap = sourceMap.get(date);
			if(PubUtil.isNotEmpty(resultMap)){
				fillList.add(resultMap);
			}else{
				Map<String, Object> tmp = new HashMap<String, Object>();
				tmp.put("REPORT_DATE", date);
				tmp.put("TREND_VAL2", "0");
				fillList.add(tmp);
			}
		}
		return fillList;
	}
	
	public List<Map<String, Object>> fillSafetySingleTendencyData(List<Map<String, Object>> list, String dateType){
		List<Map<String, Object>> fullDate = getFullDate(dateType);
		Map<String, Map<String, Object>> sourceMap = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> map : list){
			String str1 = (String)map.get("REPORT_DATE");
			sourceMap.put(str1, map);
		}
		ArrayList<Map<String, Object>> fillList = new ArrayList<Map<String, Object>>();
		for(int dsize=0;dsize<fullDate.size();dsize++){
			Map<String, Object> dMap = fullDate.get(dsize);
			String date = (String)dMap.get("TREND_DATE");
			Map<String, Object> resultMap = sourceMap.get(date);
			if(PubUtil.isNotEmpty(resultMap)){
				fillList.add(resultMap);
			}else{
				Map<String, Object> tmp = new HashMap<String, Object>();
				tmp.put("REPORT_DATE", date);
				tmp.put("TREND_VAL2", "0");
				fillList.add(tmp);
			}
		}
		return fillList;
	}
	
	public List<Map<String, Object>> fillEnergySingleTendencyData(List<Map<String, Object>> list, String dateType){
		List<Map<String, Object>> fullDate = getFullDate(dateType);
		Map<String, Map<String, Object>> sourceMap = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> map : list){
			String str1 = (String)map.get("REPORT_DATE");
			sourceMap.put(str1, map);
		}
		ArrayList<Map<String, Object>> fillList = new ArrayList<Map<String, Object>>();
		for(int dsize=0;dsize<fullDate.size();dsize++){
			Map<String, Object> dMap = fullDate.get(dsize);
			String date = (String)dMap.get("TREND_DATE");
			Map<String, Object> resultMap = sourceMap.get(date);
			if(PubUtil.isNotEmpty(resultMap)){
				fillList.add(resultMap);
			}else{
				Map<String, Object> tmp = new HashMap<String, Object>();
				tmp.put("REPORT_DATE", date);
				tmp.put("ENERGY_TREND_VAL", "0");
				fillList.add(tmp);
			}
		}
		return fillList;
	}
	
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getFullDate(String dateType){
		HashMap<String, Object> map = new HashMap<String, Object>();
		Date date = DateTool.calculateByDate(new Date(),-1);
		String dateStr = DateTool.SHORT_DATE_FORMAT.format(date);
		map.put("yesterday", dateStr);
		map.put("dateType", dateType);
		List<Map<String, Object>> list = this.sqlMapDao.selectList("homeTransit.getFullDate", map);
		return list;
	}
	
}
